﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ConsoleApplication.Utils;

namespace ConsoleApplication
{
    public class TransFormData
    {

        public static JArray paramsArray;
        public static JArray sessionArray;
        public static JObject sessionObj;
        private static string hospID = "", userID = "";
        private static JObject patParams = new JObject();


        public static JObject FindThirdOrgFieldData(string input)
        {
            patParams = new JObject();
            string productAppCode = "";
            string appPropCode = "";
            string organizeCode = "";
            string admIDs = "";
            JObject thirdOrgFieldObj = new JObject();
            paramsArray = new JArray();
            JObject param = new JObject();
            if ((input != null) && (input != ""))
            {
                string[] paraArr = input.Split(',');
                sessionArray = new JArray();
                sessionObj = new JObject();
                for (int i = 0; i < paraArr.Length; i++)
                {
                    string[] obj = paraArr[i].Split(':');
                    if (obj.Length == 2)
                    {
                        sessionObj.Add(obj[0], obj[1]);
                        if (obj[0] == "hospID")
                        {
                            hospID = obj[1];
                        }
                        if (obj[0] == "userID")
                        {
                            userID = obj[1];
                        }
                        patParams.Add(obj[0], obj[1]);
                        if (obj[0] == "productAppCode")
                        {
                            productAppCode = obj[1];
                        }
                        if (obj[0] == "appPropCode")
                        {
                            appPropCode = obj[1];
                        }
                        if (obj[0] == "organizeCode")
                        {
                            organizeCode = obj[1];
                        }
                        if (obj[0] == "admIDs")
                        {
                            admIDs = obj[1];
                        }
                    }
                }
                sessionArray.Add(sessionObj);
            }
            else { initsession(); }
            //initsession();
            param.Add("productAppCode", productAppCode != "" ? productAppCode : "OrganizAPI");
            param.Add("appPropCode", appPropCode != "" ? appPropCode : "OutHospExamine");
            param.Add("hospID", hospID);
            param.Add("userID", userID);
            param.Add("organizeCode", organizeCode != "" ? organizeCode : "EMRFRONTUPLOAD_HEFEI");
            if (param.ContainsKey("admIDs"))
            {
                param["admIDs"] = admIDs;
            }
            else
            {
                param.Add("admIDs", admIDs);
            }
            paramsArray.Add(param);

            string code = HttpsUtil.GetAppConfig("ExportOutHospExamineData");
            JObject resultObj = HttpsUtil.Post(code, paramsArray, sessionArray);
            if (resultObj != null)
            {
                if ((resultObj["errorCode"].ToString()) == "0")
                {
                    JObject result = (JObject)resultObj.GetValue("result");
                    thirdOrgFieldObj = (JObject)result.GetValue("data");
                    //foreach (var item in apijsondata)
                    //{
                    //    JArray itemAry=(JArray)item
                    //    //Console.WriteLine()
                    //    //thirdorgfieldary.add(item.value);
                    //}
                }
            }
            return thirdOrgFieldObj;
        }

        private static void initsession()
        {
            sessionArray = new JArray();
            sessionObj = new JObject();
            sessionObj.Add("groupID", "3");
            sessionObj.Add("hospID", "25");
            sessionObj.Add("hostName", "DESKTOP-2SS34FT\\ByronJames");
            sessionObj.Add("ipv4", "192.168.5.148");
            sessionObj.Add("ipv6", "fe80::9df3:c8f3:78e0:a829%9");
            sessionObj.Add("locID", "1");
            sessionObj.Add("mac", "80-30-49-89-92-37");
            sessionObj.Add("userID", "6");
            sessionObj.Add("sessionID", "KShjpk1Ssm");
            sessionObj.Add("connetedIP", "https$//np.h03.p0551.com/bdhealth/");
            sessionObj.Add("authorization", "Basic$cHJoaXA6cHJoaXBAMjAyMA==");
            sessionArray.Add(sessionObj);

            patParams.Add("wardID", "1");
            //patParams.Add("locID", "1");
            patParams.Add("admID", "20301");
            hospID = "62";
            userID = "6";
        }
    }
}
